package com.xpgk.mvc.btoShop;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.bentryRegister.CustomerProductSurplus;
import com.xpgk.mvc.binShopService.CustomerService;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productSales.ProductSales;

public class BintoShopController  extends  BaseController{
	
	private  BintoShopService  bintoShopService;
	
	public  void  index()
	{	
		Organization  org=Organization.dao.findById(getCUserOrganIds());
		Organization  parentOrg=Organization.dao.findById(org.getStr(Organization.column_parentIds));
		setAttr("organization", org);
		setAttr("parentOrg", parentOrg);
		render("bintoShop.html");
	}
	
	
	/**获取转入顾客信息
	 * 
	 */
	public  void  getIntoShop()
	{	
		
		 List<Record>  lis= bintoShopService.getIntoShop(getCUserOrganIds());
		renderJson(lis);
	}
	
	public  void  searchCus()
	{	
		
		String  outShopIds=getPara("outShopIds");
		 List<Record>  lis= bintoShopService.searchCus(getCUserOrganIds(),outShopIds);
		renderJson(lis);
	}
	
	/**获取顾客信息
	 * 
	 */
	public  void  showOutInfo()
	{
		String  customerIds=getPara("customerIds");
    	Customer  cus=Customer.dao.findById(customerIds);
    	if(cus!=null)
    	{	
    		String sql = "select * FROM sb_customer_detail WHERE customerIds = ? AND dataStatus = '5'";
    		CustomerDetail  cd=CustomerDetail.dao.findFirst(sql,cus.getStr(Customer.column_ids));
    		if(cd!=null){
    			float  sc_weight=cd.getFloat(CustomerDetail.column_firstWeight);
        		float  now_weight=cd.getFloat(CustomerDetail.column_nowWeight);
        		BtoShop  bs=BtoShop.dao.getIntoShop(customerIds, getCUserOrganIds());
        		if(bs!=null)
        		{	
            		 Organization  org=Organization.dao.findById(bs.getStr(BtoShop.column_outShopIds));
            		 Organization  rorg=Organization.dao.findById(getCUserOrganIds());
            		setAttr("reason", bs.getStr("reason"));
            		setAttr("organization", org);
            		setAttr("intoorganization", rorg);
            		setAttr("btoshop", bs);
        		}
    	    		setAttr("yijianWeight", sc_weight-now_weight);
    	    		setAttr("customer", cus);
    		}
    	
    	}
    	render("adviceNote.html");
	}
	public  void  getCusPro()
	{
		String customerIds=getPara("customerIds");
		String btoshopTime=getPara("btoshopTime");
		String outorganIds=getPara("outorganIds");
		
		List<CustomerProductSurplus>  cpsList=CustomerProductSurplus.dao.getCusProSurList(customerIds, outorganIds, "%"+btoshopTime+"%");
		renderJson(cpsList);
	}
	
	
	/**确认转入
	 * 
	 */
	@Before(Tx.class)
	public  void  saveIntoShop()
	{	
		String customerIds=getPara("customerIds");
		List<ProductSales>  cpsList=getModels(ProductSales.class);
		String outorganIds = getPara("outorganIds");
		String msg=bintoShopService.saveIntoShop(customerIds,cpsList,getCUserOrganIds(),getCUserIds(),outorganIds);
		RenderBean result = new RenderBean();
		result.setMsg(msg);
		result.setCode("200");
		renderJson(result);
	}
	
	
}
